<template>
  <transition name="searchBarComponentTransition">
    <div class="searchBarOutside" v-show="searchBarIsShown">
      <div :class="isIos?'searchBarUseHeaderBG_iOS':'searchBarUseHeaderBG'"></div>
      <div :class="isIos?'searchBarTop_iOS':'searchBarTop'"  ></div>

      <div class="searchBarBody">
        <div class="searchBar">
          <div class="searchBarLeft" @click="hideSearchBarNshowHeader">
            <van-icon name="arrow-left" color="#FFFFFF" size="1.25rem" />
          </div>
          <div class="searchBarMiddle">
            <div
              class="searchBarMiddle_leftSearchIcon"
              @click="onSearch(searchValue)"
            >
              <van-icon color="#FFFFFF" size="1.0625rem" name="search" />
            </div>
            <div class="searchBarMiddle_input">
              <form action="#" onsubmit="return false">
                <input
                  type="search"
                  placeholder="请输入搜索关键词"
                  v-model.trim="searchValue"
                  @focus="onFocus()"
                  @change="onChange()"
                  ref="inputSearch"
                  @keyup.enter="onSearch(searchValue)"
                />
              </form>
            </div>
            <div class="searchBarMiddle_rightClearIcon">
              <transition name="clearIconNcancel">
                <van-icon
                  name="clear"
                  color="#FFFFFF"
                  size="1rem"
                  v-show="showClearNcancel"
                  @click="onCancel()"
                />
              </transition>
            </div>
          </div>
          <div
            class="searchBarRight"
            @click="onSearch(searchValue)"
            v-show="showClearNcancel"
          >
            <transition name="clearIconNcancel"> <span>搜索</span></transition>
          </div>
        </div>
      </div>
    </div>

    
  </transition>
</template>

<script>
import { Toast } from "vant";
Vue.use(Toast);
export default {
  data() {
    return {
      isIos,
      clearIconIsShown: 0,
      searchValue: "",
      maskIsShown: 0,
      searchBarIsShown: false,
    };
  },
  computed: {
    showClearNcancel() {
      if (this.searchValue == "") {
        return false;
      } else {
        return true;
      }
    },
  },

  created() {
    this.searchValue=sessionStorage.getItem('searchValue')
    this.$EventBus.$on("showSearchBar", (x) => {
      if (typeof x == "boolean") {
        this.searchBarIsShown = x;
      }

      switch (x.type) {
        case "search":
          this.searchBarIsShown = true;

          break;

        default:
          break;
      }
    });
  },
  mounted() {
    this.$EventBus.$on("showSearchBar", (x) => {
      if (typeof x == "boolean") {
        this.searchBarIsShown = x;
      }

      switch (x.type) {
        case "search":
          this.searchBarIsShown = true;

          break;

        default:
          break;
      }
    });

    this.$EventBus.$on("autoFocus", () => {
      this.$nextTick(() => {
        this.$refs.inputSearch.focus();
      });
    });
  },
  activated() {
    this.searchValue=sessionStorage.getItem('searchValue')
    this.$EventBus.$on("showSearchBar", (x) => {
      if (typeof x == "boolean") {
        this.searchBarIsShown = x;
      }

      switch (x.type) {
        case "search":
          this.searchBarIsShown = true;

          break;

        default:
          break;
      }
    });

    this.$EventBus.$on("autoFocus", () => {
      this.$nextTick(() => {
        this.$refs.inputSearch.focus();
      });
    });
  },

  deactivated() {
    this.$EventBus.$emit("showHeader", !this.searchBarIsShown);
  },
  beforeDestroy() {
    this.$EventBus.$emit("showHeader", !this.searchBarIsShown);
  },

  methods: {
    onFocus() {
      this.$store.dispatch("globalMask", {
        isShow: true,
        bgColor: "rgba(0,0,0,0.3)",
        zIndex: 1500,
        origin: "fromSearchBar",
      });
    },

    hideSearchBarNshowHeader() {
      // console.log(this.$route.path);
        sessionStorage.removeItem('searchValue')
      if (this.$route.path == "/zxyh/telResult") {
        this.$router.go(-1);
      } else {
        this.$EventBus.$emit("showHeader", true);
        this.$EventBus.$emit("showSearchBar", false);
        this.searchValue == "";
        this.$store.dispatch("globalMask", {
          isShow: false,
        });
      }
    },
    onChange() {},
    onSearch(x) {
      if (x == null || x.match(/^[ ]*$/) || x == "") {
        Toast("搜索内容不能为空哦");
      } else {
        sessionStorage.setItem("searchValue", x);
      
    this.$store.dispatch("globalMask", {
          isShow: false,
        });

this.$router.push({
			        path: `/${this.$channel}/telResult`,
			        query: {
			          content: x,
			          type: '',
			        }
			      })	

        this.$emit('searchthis',x)
      }
    },

    onCancel() {
      this.searchValue = "";
    },
  },
};
</script>

<style lang="less" scoped>
.searchBarComponentTransition-leave-active,
.searchBarComponentTransition-enter-active {
  transition: opacity 0.5s;
}

.searchBarComponentTransition-enter,
.searchBarComponentTransition-leave-to {
  opacity: 0;
}
.clearIconNcancel-leave-active,
.clearIconNcancel-enter-active {
  transition: opacity 0.5s;
}

.clearIconNcancel-enter,
.clearIconNcancel-leave-to {
  opacity: 0;
}

.searchBarUseHeaderBG{
  width: 100%;
  height: 88px;
  position: fixed;
  left: 0px;
  top: 0px;
  background: url('../../zxyhPages/newHomeCom/zxyhBG.png') no-repeat center;
  background-size: cover;
}

.searchBarUseHeaderBG_iOS{
  width: 100%;
  height: 148px;
  position: fixed;
  left: 0px;
  top: 0px;
  background: url('../../zxyhPages/newHomeCom/zxyhBG.png') no-repeat center;
  background-size: cover;
}





.searchBarOutside {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 999999;
  height: 148px;

  overflow: hidden;

  .searchBarTop_iOS {
    width: 100%;
    height: 76px;
    // background: url("./top.png") no-repeat top left;
    background-size: cover;
  }
  .searchBarTop {
    width: 100%;
    height: 16px;
    // background: url("./top.png") no-repeat top left;
    background-size: cover;
  }

  .searchBarBody {
    width: 100%;
    height: 72px;
    // background: url("./body.png") no-repeat top left;
    background-size: cover;
    position: relative;
    left: 0;
    top: 0;

    .searchBar {
      width: 100%;
      position: absolute;
      background-color: rgba(255, 255, 255, 0);
      left: 0;
      top: 0;
      display: flex;
      height: 72px;
      align-items: center;

      .searchBarLeft {
        width: 74px;
        box-sizing: border-box;
        padding-left: 21px;
      }

      .searchBarMiddle {
        flex: 1;
        display: flex;
        height: 52px;
        background: rgba(255, 255, 255, 0.3);
        border-radius: 36px;
        max-width: calc(100% - 110px);
        .searchBarMiddle_leftSearchIcon {
          width: 87px;
          display: flex;
          align-items: center;
          justify-content: space-evenly;
        }

        .searchBarMiddle_input {
          flex: 1;
          height: 100%;
          form {
            width: 100%;
            height: 100%;
            input {
              background: rgba(255, 255, 255, 0);

              height: 100%;
              width: 100%;
              font-size: 28px;
              font-family: MicrosoftYaHei;
              font-weight: bold;
              color: #ffffff;
              line-height: 100%;
            }
          }

          input::-webkit-search-cancel-button {
            display: none;
          }
          input::-webkit-input-placeholder {
            /* WebKit browsers */
            color: rgba(255, 255, 255, 0.5);
          }
          input:-moz-placeholder {
            /* Mozilla Firefox 4 to 18 */
            color: rgba(255, 255, 255, 0.5);
          }
          input::-moz-placeholder {
            /* Mozilla Firefox 19+ */
            color: rgba(255, 255, 255, 0.5);
          }
          input:-ms-input-placeholder {
            /* Internet Explorer 10+ */
            color: rgba(255, 255, 255, 0.5);
          }
          input[type="search"]::-ms-clear {
            display: none;
          }
        }

        .searchBarMiddle_rightClearIcon {
          width: 72px;
          padding: 10px;
          box-sizing: border-box;
          display: flex;
          align-items: center;
          justify-content: space-evenly;
        }
      }

      .searchBarRight {
        width: 116px;
        box-sizing: border-box;

        padding-left: 29px;
        padding-top: 20px;
        padding-bottom: 20px;

        span {
          text-align: center;
          vertical-align: middle;
          font-size: 28px;
          font-family: MicrosoftYaHei;
          font-weight: bold;
          color: #ffffff;
        }
      }
    }
  }
}
</style>